package vtt.android.cartoon_video.fragments;

import java.util.ArrayList;

import vtt.android.cartoon_video.R;
import vtt.android.library.utils.VttGA;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.startapp.android.publish.Ad;
import com.startapp.android.publish.AdEventListener;
import com.startapp.android.publish.nativead.NativeAdDetails;
import com.startapp.android.publish.nativead.NativeAdPreferences;
import com.startapp.android.publish.nativead.NativeAdPreferences.NativeAdBitmapSize;
import com.startapp.android.publish.nativead.StartAppNativeAd;

public class StartAppNativeListFragment extends BaseFragment {
    
//    @InjectView(R.id.listview_ads) ListView mListViewAds;
    @InjectView(R.id.tv_header_title) TextView mTvHeader;
    
    /** StartApp Native Ad declaration */
    private StartAppNativeAd mStartAppNativeAd;
    private StartAppListAdapter mAdapter;

    private LinearLayout mRootView;
    private LayoutInflater mLayoutInflater;
    private Activity mParentAct;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        log.d("VTT>>> StartAppNativeListFragment onAttach");
        mParentAct = activity;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        log.d("VTT>>> StartAppNativeListFragment onActivityCreated");
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLayoutInflater = inflater;
        mRootView = (LinearLayout) inflater.inflate(R.layout.fragment_native_app_list, container, false);
        ButterKnife.inject(this, mRootView);
        mTvHeader.setVisibility(View.GONE);
        
        mStartAppNativeAd = new StartAppNativeAd(getActivity());
        /** 
         * Load Native Ad with the following parameters:
         * 1. Only 3 Ad
         * 2. Download ad image automatically
         * 3. Image size of 150x150px
         */
        mStartAppNativeAd.loadAd(
                new NativeAdPreferences()
                    .setAdsNumber(3)
                    .setAutoBitmapDownload(true)
                    .setImageSize(NativeAdBitmapSize.SIZE72X72),
                    nativeAdListener);
        
        return mRootView;
    }
    
    class StartAppListAdapter extends ArrayAdapter<NativeAdDetails> {
        
        private ArrayList<NativeAdDetails> itemList;
        private Context mContext;

        public StartAppListAdapter(Context context, ArrayList<NativeAdDetails> list) {
            super(context, 0, list);
            itemList = list;
            mContext = context;
        }
        
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            AdHolder holder;
            if (view == null) {
                LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = layoutInflater.inflate(R.layout.row_startapp_ads, null);
                holder = new AdHolder(view);
                view.setTag(holder);
            }
            holder = (AdHolder) view.getTag();
            NativeAdDetails item = itemList.get(position);
            if(item != null){
                holder.image.setImageBitmap(item.getImageBitmap());
                holder.title.setText(Html.fromHtml(item.getTitle()));
            }
            
            return view;
        }
    }
    
    class AdHolder {
        @InjectView(R.id.img_ads) ImageView image;
        @InjectView(R.id.tv_ads_title) TextView title;
        
        public AdHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
    
    /** Native Ad Callback */
    private AdEventListener nativeAdListener = new AdEventListener() {
        
        @Override
        public void onReceiveAd(Ad ad) {
            log.d("VTT>>> onReceiveAd");
            if(getActivity() == null){
                log.d("VTT>>> onReceiveAd getActivity() == null");
                return;
            }
            // Get the native ad
            ArrayList<NativeAdDetails> nativeAdsList = mStartAppNativeAd.getNativeAds();
            if(nativeAdsList.size() > 0){
                mTvHeader.setVisibility(View.VISIBLE);
                for (int i = 0; i < nativeAdsList.size(); i++) {
                    View adView = mLayoutInflater.inflate(R.layout.row_startapp_ads, null);
                    ImageView image = (ImageView) adView.findViewById(R.id.img_ads);
                    TextView tvTitle = (TextView) adView.findViewById(R.id.tv_ads_title);
                    TextView tvDescription = (TextView) adView.findViewById(R.id.tv_ads_description);
                    
                    final NativeAdDetails adItem = nativeAdsList.get(i);
                    image.setImageBitmap(adItem.getImageBitmap());
                    tvTitle.setText(Html.fromHtml(adItem.getTitle()));
                    tvDescription.setText(Html.fromHtml(adItem.getDescription()));
                    
                    adView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (adItem != null){
                                adItem.sendClick(mParentAct);
                                VttGA.sendEvent(mParentAct, "StartApp Click", "Native StartApp Click", adItem.getTitle(), 0);
                            }
                        }
                    });
                    
                    mRootView.addView(adView);
                    
                    // When ad is received and displayed - we MUST send impression
                    adItem.sendImpression(mParentAct);
                }
            }
        }
        
        @Override
        public void onFailedToReceiveAd(Ad ad) {
            
        }
    };
}
